<?php

namespace app\common\service;

use OSS\Core\OssException;
use OSS\OssClient;

class OssService
{
    /**
     *阿里云
     */
    static function alYunOSS($filePath, $Extension, $path)
    {
        $data = get_config('file');
        $accessKeyId = $data['file-accessKeyId'];
        $accessKeySecret = $data['file-accessKeySecret'];
        $endpoint = $data['file-endpoint'];
        $bucket = $data['file-OssName'];
        $object = $path.'/'.date("Ymd").'/'.time().rand(10000, 99999).'.'.$Extension;    // 文件名称
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, true);
            $rel = $ossClient->uploadFile($bucket, $object, $filePath);
            return ['code' => 200, 'src' => $rel["info"]["url"]];
        } catch (OssException $e) {
            return ['code' => 201, 'msg' => $e->getMessage()];
        }
    }
}